import api from '../../../api/index';
import common from '../../../common/index';

Page({
    data: {
        statusBarHeight: 0,
        navBarHeight: 0,
        activeTab: 'following', // 当前激活的tab
        followingList: [], // 关注列表
        followersList: [], // 粉丝列表
        uid: '', // 当前用户uid
        loading: false
    },
  
    onLoad: function (options) {
        // 根据传入参数设置初始tab
        if (options.initialTab) {
            this.setData({
                activeTab: options.initialTab
            });
        }
        
        const app = getApp();
        this.setData({
            statusBarHeight: app.globalData.statusBarHeight,
            navBarHeight: app.globalData.navBarHeight
        });
        
        // 获取用户uid
        const user = common.getAppUserInfo();
        this.setData({
            uid: (user && user.uid) ? user.uid : ''
        });
      
        // 加载数据
        this.loadData();
    },
  
    // 切换Tab
    switchTab: function(e) {
        const tab = e.currentTarget.dataset.tab;
        this.setData({
            activeTab: tab
        });
        
        // 切换tab后重新加载对应数据
        this.loadData();
    },
  
    // 加载数据
    async loadData() {
        if (!this.data.uid) {
            console.error('用户uid不存在');
            return;
        }
        
        this.setData({ loading: true });
        
        try {
            console.log('=== 调用getFollowInfo接口 ===');
            const res = await api.getFollowInfo({ uid: this.data.uid });
            console.log('getFollowInfo接口返回:', res);
            
            if (res.status === "success") {
                const { following_list = [], follower_list = [] } = res;
                
                // 处理头像URL
                const processedFollowingList = following_list.map(item => ({
                    ...item,
                    icon: item.icon ? 'https://www.aispire.info' + item.icon.replace(/.*?(?=\/user_portrait)/, '') : '../../../assets/image/def-icon.png'
                }));
                
                const processedFollowersList = follower_list.map(item => ({
                    ...item,
                    icon: item.icon ? 'https://www.aispire.info' + item.icon.replace(/.*?(?=\/user_portrait)/, '') : '../../../assets/image/def-icon.png'
                }));
                
                this.setData({
                    followingList: processedFollowingList,
                    followersList: processedFollowersList
                });
                
                console.log('处理后的关注列表:', processedFollowingList);
                console.log('处理后的粉丝列表:', processedFollowersList);
            }
        } catch (error) {
            console.error('获取关注信息失败:', error);
            wx.showToast({
                title: '获取数据失败',
                icon: 'error'
            });
        } finally {
            this.setData({ loading: false });
        }
    },
  
    // 取消关注
    async onUnfollowClick(e: any) {
        const followUid = e.currentTarget.dataset.uid;
        
        if (!followUid) {
            wx.showToast({
                title: '用户信息不完整',
                icon: 'error'
            });
            return;
        }
        
        wx.showModal({
            title: '确认取消关注',
            content: '确定要取消关注该用户吗？',
            success: async (res) => {
                if (res.confirm) {
                    try {
                        console.log('=== 调用followUser接口取消关注 ===');
                        const params = {
                            uid: this.data.uid,
                            action: 'cancel',
                            follow_uid: followUid
                        };
                        console.log('取消关注参数:', params);
                        
                        const result = await api.followUser(params);
                        console.log('取消关注接口返回:', result);
                        
                        if (result.status === 'success') {
                            wx.showToast({
                                title: '已取消关注',
                                icon: 'success'
                            });
                            
                            // 重新加载数据
                            this.loadData();
                        } else {
                            wx.showToast({
                                title: result.message || '操作失败',
                                icon: 'error'
                            });
                        }
                    } catch (error) {
                        console.error('取消关注失败:', error);
                        wx.showToast({
                            title: '网络错误，请重试',
                            icon: 'error'
                        });
                    }
                }
            }
        });
    },
    
    // 返回
    goBack: function() {
        wx.navigateBack();
    }
});